package com.dq.rbac.service.impl;

import com.dq.rbac.entity.SysMenu;
import com.dq.rbac.service.SysMenuService;
import com.dq.rbac.mapper.SysMenuMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (SysMenu)表服务实现类
 *
 * @author Dq
 * @since 2022-12-27 13:53:04
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    public List<SysMenu> buildTreeMenu(List<SysMenu> sysMenuList) {
        ArrayList<SysMenu> resultMenus = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            //查询是否为子权限
            for (SysMenu childrenMenu : sysMenuList) {
                if (childrenMenu.getParentId() == sysMenu.getId()) {
                    sysMenu.getChildren().add(childrenMenu);
                }
            }
            //查询是否为一级权限
            if (sysMenu.getParentId() == 0L) {
                resultMenus.add(sysMenu);
            }
        }
        return resultMenus;
    }
}